<template>
  <div class="product-list">
    <h2>{{title}}</h2>
    <div class="list">
      <div class="item" v-for="product in productArr" :key="product.id"  @click="goToDetails">
        <img :src="product.image_main" alt="" @error="getError" :data-id="product.id" >
        <h4>{{product.name}}</h4>
        <p>{{product.intro}}</p>
      </div>
    </div>
  </div>
</template>

<script>
import {useRouter} from "vue-router";
import logoImg from "../../assets/logo-error-small.png"
export default {
  name: "ProductList",
  props:{
    productArr:{
      type:Array,
      default:[]
    },
    title:{
      type:String,
      default: "Product"
    }
  },
  setup(){
    const router = new useRouter()
    const getError = (e) => {
      e.target.src = logoImg
    }
    const goToDetails = (el)=>{
      if(el.target.dataset.id){
        router.push({path:'/product/detail',query:{id:el.target.dataset.id*1}});
      }
    }
    return {
      getError,
      goToDetails
    }
  }
}
</script>

<style scoped lang="scss">
  .product-list{
    margin-bottom: 20px;
    h2{
      font-size:30px;
      line-height: 80px;
      text-align:center;
    }
    .list{
      margin:0 auto;
      width:70vw;
      display:grid;
      grid-template-columns: repeat(4, 1fr);
      grid-gap: 20px;
      text-align: left;
      img{
        width:100%;
        height:auto;
        cursor:pointer;
      }
      h4{
        font-size:16px;
        color:black;
        margin: 15px 0;
      }
      p{
        color:#737373;
        font-size: 14px;
      }
    }
  }
</style>
